#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

static char pwm_path[100];

/*
    构造函数，进行PWM文件打开与写入操作
*/
static int pwm_config(const char *attr, const char *val)
{
    char file_path[100];
    int len;
    int fd;

    /*打开路径下文件*/
    sprintf(file_path,"%s/%s",pwm_path,attr);
    fd = open(file_path,O_WRONLY);
    if(fd < 0){
        perror("open error");
        return fd;
    }

    /*向文件中写入控制参数*/
    len = strlen(val);
    if(len != write(fd,val,len)){
        perror("write error");
        close(fd);
        return -1;
    }

    /*关闭文件*/
    close(fd);
    return 0;
}

int main(int argc, char*argv[])
{
    /*校验传参*/
    if(4!=argc){
        fprintf(stderr,"usage:%s<id><peiod><duty>\n",argv[0]);
        exit(-1);
    }

    /*打印配置信息*/
    printf("PWM config: id<%s>, period<%s>, duty<%s>\n",argv[1], argv[2],argv[3]);

    /*导出pwm*/
    sprintf(pwm_path, "/sys/class/pwm/pwmchip%s/pwm0", argv[1]);

    if (access(pwm_path, F_OK)) {//如果 pwm0 目录不存在, 则导出
    
        char temp[100];
        int fd;

        sprintf(temp, "/sys/class/pwm/pwmchip%s/export", argv[1]);

        if (0 > (fd = open(temp, O_WRONLY))) {
            perror("open error");
            exit(-1);
        }

        if (1 != write(fd, "0", 1)) {//导出 pwm
            perror("write error");
            close(fd);
            exit(-1);
        }

        close(fd); 
    }

    /*配置PWM周期*/
    if (pwm_config("period", argv[2]))
        exit(-1);

    /* 配置占空比 */
    if (pwm_config("duty_cycle", argv[3]))
        exit(-1);

    /* 使能 pwm */
    pwm_config("enable", "1");

    /* 退出程序 */
    exit(0);

}